package com.guo.rpc.client.handler;

import com.guo.rpc.codec.ServiceRemoteResponse;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.Callable;

/**
 * @Author: ljguo4
 * @email: ljguo4@iflytek.com
 * @Date: 2021/7/15 19:47
 * @Description:
 * @version: 1.0
 */
@ChannelHandler.Sharable
public class ServiceRemoteInvokeResponseHandler extends SimpleChannelInboundHandler<ServiceRemoteResponse> implements Callable {

    public static ServiceRemoteInvokeResponseHandler INSTANCE = new ServiceRemoteInvokeResponseHandler();

    private Object resultValue;
    @Override
    protected synchronized void channelRead0(ChannelHandlerContext ctx, ServiceRemoteResponse msg) throws Exception {
        Object resultValue = msg.getResultValue();
        Class resultType = msg.getResultType();
        this.resultValue = resultValue;
        notify();
    }

    @Override
    public synchronized Object call() throws Exception {
        wait();
        return resultValue;
    }
}
